<?php

namespace app\admin\controller;

use app\models\{Admin as model, AdminLogin, Region};
use app\validate\Admin as Validate;
use think\Db;
use think\facade\Debug;

class Admin extends Base
{
    public function index()
    {
        if ($this->request->isAjax()) {
            $model = new model();
            $post = input();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $where = array();
            $where[] = ['id', '<>', $this->request->admin['id']];
            $where[] = ['del', '=', 1];
            if (!empty($post['title'])) {
                $where[] = ['nickname|username', 'like', '%' . $post['title'] . '%'];
            }
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("id desc")->limit($limit)->page($page)->select();
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            return view('', []);
        }
    }

    public function input()
    {
        if ($this->request->isPost()) {
            $data = $this->request->post('data');
            $validate = new Validate();
            if (!$validate->scene('add')->check($data) && empty($data['id'])) {
                return json(['code' => 1, 'msg' => $validate->getError()]);
            }
            if (!$validate->scene('edit')->check($data) && !empty($data['id'])) {
                return json(['code' => 1, 'msg' => $validate->getError()]);
            }
            $haveadmin = (new model())->where('username', $data['username'])->find();
            if (!empty($haveadmin) && $haveadmin['id'] != $data['id']) {
                return json(['code' => 1, 'msg' => '账号名称已被使用']);
            }
            $data['role'] = implode(',', $data['role'] ?? []);
            if (isset($data['password']) && $data['password']) {
                $entry = $this->get_rand_str();
                $data['entry'] = $entry;
                $data['password'] = md5(md5($data['password']) . md5($entry));
            } else {
                unset($data['password']);
            }
            if ($data['id'] > 0) {
                $res = (new model())->allowfield(true)->save($data, ['id' => $data['id']]);
            } else {
                $res = (new model())->allowfield(true)->save($data);
            }
            if ($res) {
                return json(['code' => 200, 'msg' => '操作成功']);
            }
            return json(['code' => 1, 'msg' => '操作失败,请重试']);
        } else {
            $id = $this->request->get('id/d', 0);
            $row = (new model())->where('id', $id)->find();
            if (!empty($row)) {
                $row['role'] = array_filter(explode(',', $row['role']));
            } else {
                $data['role'] = [];
            }
            return view('', [
                'data' => $row,
                'role' => $this->request->admin['role']
            ]);
        }
    }

    public function del()
    {
        $admin_id =  $this->request->get('id');
        $admin = (new model())->where('id', $admin_id)->find();
        if(empty($admin)){
            return json(['status' => 1, 'msg' => '账号不存在']);            
        }
        if ($admin['is_default'] == 1) {
            return json(['status' => 1, 'msg' => '超级管理员不允许禁用']);
        }
        if($admin['del'] == 1){
            $del = 2;
        }else{
            $del = 1;
        }
        $res = (new model())->where('id', $admin_id)->update(['del' => $del]);
        if ($res) {
            return json(['code' => 200, 'msg' => '操作成功']);
        }
        return json(['code' => 1, 'msg' => '操作失败']);
    }

    public function login_log()
    {
        if ($this->request->isAjax()) {
            $model = new AdminLogin();
            $post = input();
            $page = input('page/d') ?? 1;
            $limit = input('limit/d') ?? 10;
            $where = array();
            $where[] = ['uid', '=', $this->request->admin['id']];
            if (!empty($post['title'])) {
                $where[] = ['username', 'like', '%' . $post['title'] . '%'];
            }
            $count = $model->where($where)->count();
            $page = ($count <= $limit) ? 1 : $page;
            $list = $model->where($where)->order("id asc")->limit($limit)->page($page)->select();
            return json(['code' => 200, 'msg' => '获取成功', 'data' => $list, 'count' => $count]);
        } else {
            return view('', []);
        }
    }
}
